using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Walkmen.Util;

namespace Walkmen.ID3v2
{
    public class ID3v2TextFrame: ID3v2Frame
    {
        public const string FRAME_ID_TPE1 = "TPE1";
        public const string FRAME_ID_TIT2 = "TIT2";
        public const string FRAME_ID_TALB = "TALB";
        public const string FRAME_ID_TRCK = "TRCK";
        public const string FRAME_ID_TYER = "TYER";
        
        private ID3v2Encoding _encoding;
        private string _value;

        public ID3v2Encoding Encoding
        {
            get { return _encoding; }
            set { _encoding = value; }
        }

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        protected override byte[] CreateValue()
        {
            Encoding encoding = CodeToEncoding(_encoding);

            using (MemoryStream stream = new MemoryStream())
            {
                stream.WriteByte((byte)_encoding);

                byte[] preamble = encoding.GetPreamble();
                stream.Write(preamble, 0, preamble.Length);

                byte[] text = encoding.GetBytes(_value + '\0');
                stream.Write(text, 0, text.Length);

                return stream.ToArray();
            }
        }
    }
}
